%Simon
%Alejo Salles
%01 Apr 11

% Simon game
% Takes screen resolution as input, default 1440 x 900
% Might need to customize keycodes, use KbStrokeWait() as below
% Sound uses PsychPortAudio, which might not work under Linux..

function Simon(resolution)


%screen parameters;
scrNum=0;
%resolution=[1920 1080]; 
if nargin<1
    resolution=[1600 900];%change this to your screen size or pass it as an argument
end
width=resolution(1);
height=resolution(2);
win=Screen('OpenWindow', scrNum, 0);

%keycodes; might need to recalibrate this
%upkey=82;
%downkey=81;
%leftkey=80;
%rightkey=79;

upkey=112;
downkey=117;
leftkey=114;
rightkey=115;

allkeys=[upkey; leftkey; rightkey; downkey]; %order matches allpositions below

%to calibrate keys use
%[secs, keycode,delta]=KbStrokeWait(); %get a key press
%index=find(keycode) %get index of pressed key


%colors
red=[55 0 0];
gre=[0 55 0];
blu=[0 0 55];
yel=[55 55 0];

brired=[255 0 0];
brigre=[0 255 0];
briblu=[0 0 255];
briyel=[255 255 0];

%positions
top=round([6*width height 10*width 5*height]/16);  
left=round([width 6*height 5*width 10*height]/16);
right=round([11*width 6*height 15*width 10*height]/16);
bottom=round([6*width 11*height 10*width 15*height]/16);

%sounds
%short (for Simon's turn)
topsou=sin(0.5*(1:3000));
leftsou=sin(0.3*(1:3000));
rightsou=sin(0.4*(1:3000));
bottomsou=sin(0.2*(1:3000));

%long (to be truncated, for player's turn)
longtopsou=sin(0.5*(1:50000));
longleftsou=sin(0.3*(1:50000));
longrightsou=sin(0.4*(1:50000));
longbottomsou=sin(0.2*(1:50000));


%draw start screen
allcolors=[red; blu; gre; yel];
allbricolors=[brired; briblu; brigre; briyel];
allpositions=[top; left; right; bottom]; %order matches allkeys above
allsounds=[topsou; leftsou; rightsou; bottomsou];
alllongsounds=[longtopsou; longleftsou; longrightsou; longbottomsou];
Screen('FillRect',win,allcolors',allpositions');
Screen('TextSize',win,28);
Screen('DrawText',win, 'Bienvenido al Simon',width/2-140,height/2-100,255); 
Screen('DrawText',win, 'Use las flechas para jugar',width/2-180,height/2,255);
Screen('DrawText',win, 'Presione cualquier tecla para empezar',width/2-260,height/2+50,255);


Screen('Flip',win);
HideCursor();

%open sound channel and play welcome sound (prevents later latency on first round)
%Snd('Open'); %this is if PsychPortAudio does not work, linux?
%soundchannel=PsychPortAudio('Open',2,1,0,8000,1);
%soundchannel=PsychPortAudio('Open');
%welcomeSound=sin(kron((0.1:0.1:0.9),(1:10000)));
%PsychPortAudio('FillBuffer',soundchannel, welcomeSound);
%PsychPortAudio('Start',soundchannel);

KbStrokeWait();

%Play!

WaitSecs(0.6);

won=1;
game=0;

while won

%Simon plays
newplay=ceil(rand*4);
game=[game newplay];

for i=2:length(game)
    
newcolors=allcolors;
newcolors(game(i),:)=allbricolors(game(i),:);

Screen('FillRect',win,newcolors',allpositions');
Screen('Flip',win);

%play sound
%Snd('Play', allsounds(game(i),:)); %no psychport version
%PsychPortAudio('FillBuffer',soundchannel, allsounds(game(i),:));
%PsychPortAudio('Start',soundchannel);

%wait needed if no sound play?
WaitSecs(0.3);

Screen('FillRect',win,allcolors',allpositions');
Screen('Flip',win);

WaitSecs(0.2);


end



%user plays
for j=2:length(game)
    
amount=0;
while amount==0
    [amount, ~, keycode]=KbCheck;
end

%check for cheaters!
if(amount~=1)
disp('too many keys pressed! trying to cheat? :-) ');
ShowCursor;
Screen('CloseAll');
return;
end

index=find(keycode);
pressed=find(allkeys==index);

if(isempty(pressed))
    disp('wrong key!');
    ShowCursor;
    Screen('CloseAll');
    return;
end

    

usercolors=allcolors;
usercolors(pressed,:)=allbricolors(pressed,:);

Screen('FillRect',win,usercolors',allpositions');
Screen('Flip',win);

%play sound
%Snd('Play', allsounds(pressed,:)); %no psychport version, fixed time
%PsychPortAudio('FillBuffer',soundchannel, alllongsounds(pressed,:));
%PsychPortAudio('Start',soundchannel);


while amount==1
    [amount, ~, keycode]=KbCheck;
end

%stop sound
%PsychPortAudio('Stop',soundchannel);



Screen('FillRect',win,allcolors',allpositions');
Screen('Flip',win);


%check if wrong
if allkeys(game(j))~=index
    
    aciertos=int2str(length(game)-2);
    
    Screen('DrawText',win, 'Error!',width/2-100,height/2-150,255); 
    if aciertos=='1'
        Screen('DrawText', win, 'Tuviste 1 acierto!', width/2-180,height/2-100,255);
    else
        Screen('DrawText', win, ['Tuviste ' aciertos ' aciertos!'], width/2-180,height/2-100,255);
    end
    Screen('DrawText',win, 'Aprete s para salir, cualquier otra tecla para jugar de nuevo..',width/2-450,height/2,255);
    Screen('Flip',win);
    
    [~, answer, ~]=KbStrokeWait();
    
    %if find(answer)==22
    if find(answer)==40

        Screen('DrawText',win, 'Gracias por jugar :-)',width/2-150,height/2,255);
        Screen('Flip',win);
        
        WaitSecs(0.8);
        
        ShowCursor();
        Screen('CloseAll');
        return;
    end
    
    game=0;
    break;
    
end

WaitSecs(0.1);

end

WaitSecs(0.5);

end

ShowCursor();
Screen('CloseAll');



return 